package i.i.myapplication;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class MineFragment extends Fragment {



    @Nullable
    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater,
            @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState
    ) {
        // 1. 使用 inflater 加载布局文件
        View view = inflater.inflate(
                R.layout.fragment_mine,  // 布局文件资源 ID
                container,               // 父容器
                false                    // 不立即附加到父容器
        );

        // 2. 返回加载后的视图
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 3. 在此处初始化 UI 组件（如 TextView、Button）
        // TextView tvHome = view.findViewById(R.id.tv_home);
        // tvHome.setText("Hello, Fragment!");


        TextView userAccountTextView = view.findViewById(R.id.user_account);
        TextView userTelTextView = view.findViewById(R.id.tel);

        // 从 Room 中获取用户数据
        new Thread(() -> {
            i.i.myapplication.AppDatabase db = i.i.myapplication.AppDatabase.getInstance(requireContext());
            i.i.myapplication.User user = db.userDao().getSingleUser(); // 获取单条用户记录

            if (user != null) {
                requireActivity().runOnUiThread(() -> {
                    userAccountTextView.setText(user.userId);
                    userTelTextView.setText(user.phoneNumber);
                });
            } else {
                requireActivity().runOnUiThread(() -> {
                    userAccountTextView.setText("无数据");
                    userTelTextView.setText("无数据");
                });
            }
        }).start();


        LinearLayout logOut = view.findViewById(R.id.layout_loginOut);
        logOut.setOnClickListener(v -> {
            // 这里是点击后的处理逻辑
            Toast.makeText(requireContext(), "退出登录!", Toast.LENGTH_SHORT).show();

            new Thread(() -> {
//                AppDatabase db = AppDatabase.getInstance(requireContext());
//                db.userDao().deleteAllUsers(); // 删除所有用户记录

                // 清除 SharedPreferences 中保存的登录信息
                SharedPreferences prefs = requireContext().getSharedPreferences("login_prefs", Context.MODE_PRIVATE);
                prefs.edit().clear().apply();

                requireActivity().runOnUiThread(() -> {
                    Toast.makeText(requireContext(), "已退出登录", Toast.LENGTH_SHORT).show();
                    // 跳转到登录页面
                    Intent intent = new Intent(requireContext(), i.i.myapplication.LoginActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // 清除返回栈
                    startActivity(intent);
                });
            }).start();
        });

        LinearLayout layoutClinicianLogin = view.findViewById(R.id.layout_clinician_login);

        // 设置点击事件
        layoutClinicianLogin.setOnClickListener(v -> {
            // 这里是点击后的处理逻辑
            Toast.makeText(requireContext(), "临床医生登录!", Toast.LENGTH_SHORT).show();
//            Intent intent = new Intent(requireContext(), ClinicianActivity.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // 清除返回栈
//            startActivity(intent);

        });

    }
}

